<?php
/*

Class Google search
@author Rana

*/

require_once("functions.php");

class Google {
	
	//All search request URLs start with the hostname and resemble the following sample:
	var $hostname;
	
	var $baseurl;

	var $livesite;

	var $contents;



	function Google() {
		$this->hostname = $GLOBALS['google_hostname'];
		$this->livesite = $GLOBALS['google_live'];
		$this->baseurl = $this->hostname;
	}
	
	function frame_link($url) {
		$this->baseurl .= $url;
	}
	
	function get_response($url) {
		if (empty($url)) return false;
		//$this->frame_link($url);
		$url = $this->baseurl . $url;
		
		//if (empty($this->contents))
		$this->contents = file_get_contents($url);
		
		$results = 0;
		
		if ($this->contents !== false) {
			$regexp = $GLOBALS['google_regexp'];
			$contents = strip_tags($this->contents);
			
			if (preg_match($regexp, $contents, $matches) ) {
				//print_r($matches);
				if (isset($matches[3])) {
					$results = str_replace(",","",$matches[3]);
				}
			} 
		
		}
		return $results;
	}

	//
	function get_url($value, $url) {
		$url = '<a href="'.$this->livesite.$url.'" title="Google" class="link1" target="_blank">'.$value.'</a>';

		return $url;
	}
	
	//
	function get_links($url) {
		if (empty($url)) return false;

		$url = $this->baseurl . $url;
		
		if (empty($this->contents))
			$this->contents = file_get_contents($url);

		//$this->contents = preg_replace('/<blockquote class=g>(.*)<\/blockquote>/is','',$this->contents);

		$results = array();
		
		if ($this->contents !== false) {

			$links = get_html_tags($this->contents,'a');
			$i = 0;

			foreach ($links as $link) {
				if (isset($link['attributes']['class']) && $link['attributes']['class'] == 'l') {

					$results[$i]['link'] = $link['attributes']['href'];
					$results[$i]['atext'] = $link['text'];
					$i++;
				}
			}
		}
		return $results;
	}
}

?>